package com.example.javatest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javatest.entity.domain.InfoClass;
import com.example.javatest.entity.domain.TbClass;
import com.example.javatest.entity.domain.TbUser;
import com.example.javatest.mapper.TbClassMapper;
import com.example.javatest.mapper.TbUserMapper;
import com.example.javatest.service.InfoClassService;
import com.example.javatest.mapper.InfoClassMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author cac_qin
* @description 针对表【info_class】的数据库操作Service实现
* @createDate 2023-12-14 11:53:56
*/
@Service
public class InfoClassServiceImpl extends ServiceImpl<InfoClassMapper, InfoClass>
    implements InfoClassService{

//    @Resource
//    InfoClassMapper infoClassMapper;

    @Resource
    TbClassMapper tbClassMapper;

    @Resource
    TbUserMapper tbUserMapper;

    @Resource
    InfoClassMapper infoClassMapper;

    /**
     * 新建班级消息
     * @param infoTitle
     * @param infoText
     * @param master
     * @param classId
     * @return
     */
    @Override
    public boolean create(String infoTitle, String infoText, String master, int classId) {

        //查询教师是否存在
        QueryWrapper<TbUser> queryWrapperUser = new QueryWrapper<>();
        queryWrapperUser.eq("user_Name",master);
        int countUser = tbUserMapper.selectCount(queryWrapperUser);
        if(countUser<0){
            System.out.println("教师不存在");
            return false;
        }

        //查询班级是否存在
        QueryWrapper<TbClass> queryWrapperClass = new QueryWrapper<>();
        queryWrapperClass.eq("Class_Id",classId);
        int countClass = tbClassMapper.selectCount(queryWrapperClass);
        if(countClass<0){
            System.out.println("班级不存在");
            return false;
        }

        //保存信息
        InfoClass infoClass = new InfoClass();
        infoClass.setClassId(classId);
        infoClass.setInfoMaster(master);
        infoClass.setInfoTitle(infoTitle);
        infoClass.setInfoText(infoText);
        boolean result =this.save(infoClass);
        return result;
    }

    @Override
    public List<InfoClass> searchClassInfoByID(int classID,String searchKey) {
        QueryWrapper<InfoClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Class_Id",classID)
                .eq("isDelete",3)
                .and(Wrapper->Wrapper
                        .like("info_Title",searchKey)
                        .or()
                        .like("info_text",searchKey));


        int count = infoClassMapper.selectCount(queryWrapper);
        if(count<0){
            return null;
        }
        return infoClassMapper.selectList(queryWrapper);
    }

    @Override
    public boolean sure(int infoId) {
        QueryWrapper<InfoClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("info_Id",infoId);
        InfoClass infoClass = infoClassMapper.selectOne(queryWrapper);
        infoClass.setIsdelete(3);
        infoClassMapper.updateById(infoClass);
        return true;
    }

    @Override
    public boolean unSure(int infoId) {
        QueryWrapper<InfoClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("info_Id",infoId);
        InfoClass infoClass = infoClassMapper.selectOne(queryWrapper);
        infoClass.setIsdelete(1);
        infoClassMapper.updateById(infoClass);
        return true;
    }
}




